//
//  CommodityListViewController.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/4/21.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "CommodityListViewController.h"
#import "CommodityInFoViewController.h"
#import "CosMeticListTableViewCell.h"
#import "InterFace.h"
#import "ProductListModel.h"
#import "CategoryModel.h"
#import "ProductModel.h"
#import "GMDCircleLoader.h"

#import "SDRefresh.h"
#import "UIScrollView+FCXRefresh.h"
#import "FCXRefreshFooterView.h"
#import "FCXRefreshHeaderView.h"
@interface CommodityListViewController ()<ProductListModelDelegate>
{
     CommodityListViewController*connVc;
     UILabel *_title;
    NSInteger pageWithNotDeliver;//页数
      FCXRefreshFooterView *footerView;
}
@property (nonatomic, strong) NSMutableArray *productlistArr;
@property (nonatomic, weak) SDRefreshFooterView *refreshFooter;
@end
@implementation CommodityListViewController

- (void)dealloc
{
    [self.refreshFooter removeFromSuperview];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];
  

    
    //开始获取商品列表数据
    self.productlistArr = [NSMutableArray array];
    ProductListModel *model = [[ProductListModel alloc] init];
    model.productlistModelDelegate = self;
    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
 
    
    if (self.categoryModel == nil) {
        NSDictionary *parameterDic = @{@"TagName":self.productModel.ProductName,@"AgentID":agentId};
        [model getProductList:self.productlistArr parameter:parameterDic];
    } else{
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"CategoryID":self.categoryModel.CategoryID};
        [model getCategoryList:self.productlistArr parameter:parameterDic];
    }

        
    [self createBackButton];
    
 
    [self createCommodityListTableView];
    

    
     connVc = self;
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    
    [self addRefreshView];
    
    
}


- (void)addRefreshView {
    
    __weak __typeof(self)weakSelf = self;
    //上拉加载更多
    footerView = [self.commodityListTableView addFooterWithRefreshHandler:^(FCXRefreshBaseView *refreshView) {
        [weakSelf loadMoreAction];
    }];
    
}
- (void)loadMoreAction {
    pageWithNotDeliver++;
    __weak UITableView *weakTableView =  self.commodityListTableView;
    __weak FCXRefreshFooterView *weakFooterView = footerView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *pageNum = [NSString stringWithFormat:@"%ld",pageWithNotDeliver];
        ProductListModel *model = [[ProductListModel alloc] init];
        model.productlistModelDelegate = self;
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
          [self.productlistArr removeAllObjects];
        if (self.categoryModel == nil) {
            NSDictionary *parameterDic = @{@"TagName":self.productModel.ProductName,@"AgentID":agentId,@"PageID":pageNum};
            [model getProductList:self.productlistArr parameter:parameterDic];
        } else{
            NSDictionary *parameterDic = @{@"AgentID":agentId,@"CategoryID":self.categoryModel.CategoryID,@"PageID":pageNum};
            [model getCategoryList:self.productlistArr parameter:parameterDic];
        }
        self.commodityListTableView.frame = CGRectMake(0, 64,  SCREEN_WIDTH, SCREEN_HEIGHT-64-40);
        [weakTableView reloadData];
        [weakFooterView endRefresh];
    });
}



#pragma -mmark 创建返回按钮
-(void)createBackButton{

    
    UIView *qview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
     backBut.frame = CGRectMake(-25, -2, 85, 44);    
    [backBut setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backClick23) forControlEvents:UIControlEventTouchUpInside];
    [qview addSubview:backBut];
    
    
    _title = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-110, 5, 200, 30)];
    _title.textAlignment = NSTextAlignmentCenter;
    _title.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    _title.text = self.mytitle;
    _title.textColor = [UIColor whiteColor];
    [qview addSubview:_title];
    self.navigationItem.titleView = qview;
    
}

#pragma -mark /获取商品列表类型接口结束代理方法
- (void)getProDuctListInterfaceEnd{
    
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _title.text = self.productModel.ProductName;
    [self.commodityListTableView reloadData];
    
}

#pragma -mark 获取商品类列表接口结束
- (void)getCategoryListInterfaceEnd{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _title.text = self.categoryModel.CategoryName;
    
    
    [self.commodityListTableView reloadData];
    
}

#pragma -mark 返回按钮事件
- (void)backClick23{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark //请求服务器繁华的错误信息代理
- (void)getrequesterror:(NSString *)error{
     [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([error isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:error delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma -mark 网络异常代理／服务器起错误代理
- (void)requesterror{
     [GMDCircleLoader hideFromView:self.view animated:YES];
     [_refreshFooter endRefreshing];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma -mark 创建商品列表
- (void)createCommodityListTableView{
    self.commodityListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,  SCREEN_WIDTH, SCREEN_HEIGHT-40)];
    self.commodityListTableView.delegate = self;
    self.commodityListTableView.dataSource = self;
    self.commodityListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.commodityListTableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.productlistArr count];
//    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden = @"shoppingCell";
    CosMeticListTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_mall_xibs" owner:self options:nil] objectAtIndex:4];
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
//    cell.textLabel.text = @"测试";
        [cell setMeticlistViewData:self.productlistArr[indexPath.row]];
        return cell;
}

#pragma -mark 跳转到商品详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed = YES;
    CommodityInFoViewController *commodityInfo = [[CommodityInFoViewController alloc] init];
    commodityInfo.productlistModel = self.productlistArr[indexPath.row];
    commodityInfo.commVC = self;
  
    [self.navigationController pushViewController:commodityInfo animated:YES];
}



@end
